package com.ssy.lingxi.logistics.serviceimpl;

import com.ssy.lingxi.logistics.entity.Company;
import com.ssy.lingxi.logistics.entity.LogisticsOrder;
import com.ssy.lingxi.logistics.model.enums.LogisticsOrderStatusEnum;
import com.ssy.lingxi.logistics.model.enums.OrderLogisticsTypeEnum;
import com.ssy.lingxi.logistics.repository.CompanyRepository;
import com.ssy.lingxi.logistics.service.ILogisticsOrderNotifyService;
import com.ssy.lingxi.message.api.constant.MessageTemplateCode;
import com.ssy.lingxi.message.api.feign.SystemMessageControllerFeign;
import com.ssy.lingxi.message.api.vo.request.SystemMessageRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 物流单消息通知服务实现类
 *
 * @author yzc
 * @version 2.0.0
 * @date 2021/1/26
 */
@Slf4j
@Service
public class LogisticsOrderNotifyServiceImpl implements ILogisticsOrderNotifyService {

    @Resource
    private CompanyRepository companyRepository;
    @Resource
    private SystemMessageControllerFeign systemMessageControllerFeign;

    @Override
    public void submitMessage(LogisticsOrder logisticsOrder) {
        Company company = companyRepository.findById(logisticsOrder.getCompanyId()).orElse(new Company());
        SystemMessageRequest systemMessageRequest = new SystemMessageRequest();
        systemMessageRequest.setMemberId(company.getCompanyMemberId());
        systemMessageRequest.setRoleId(company.getCompanyRoleId());
        systemMessageRequest.setMessageNotice(MessageTemplateCode.logistics_order_confirm);
        systemMessageRequest.setParams(Arrays.asList(logisticsOrder.getLogisticsOrderNo()));

        try {
            systemMessageControllerFeign.sendSystemMessage(systemMessageRequest);
        } catch (Exception e) {
            log.error("物流单提交 - 发送消息", e);
        }
    }

    @Override
    public void confirmMessage(LogisticsOrder logisticsOrder, Integer status) {
        SystemMessageRequest systemMessageRequest = new SystemMessageRequest();
        systemMessageRequest.setMemberId(logisticsOrder.getMemberId());
        systemMessageRequest.setRoleId(logisticsOrder.getRoleId());
        systemMessageRequest.setMessageNotice(MessageTemplateCode.logistics_order_submit);

        List<String> params;
        if (LogisticsOrderStatusEnum.RECEIVE.getCode().equals(status)) {
            // 接受
            params = Arrays.asList(logisticsOrder.getLogisticsOrderNo(), logisticsOrder.getCompanyName(), OrderLogisticsTypeEnum.ACCEPT.getName());
        } else {
            // 拒绝
            params = Arrays.asList(logisticsOrder.getLogisticsOrderNo(), logisticsOrder.getCompanyName(),OrderLogisticsTypeEnum.NOT_ACCEPT.getName());
        }
        systemMessageRequest.setParams(params);

        try {
            systemMessageControllerFeign.sendSystemMessage(systemMessageRequest);
        } catch (Exception e) {
            log.error("物流单确认 - 发送消息", e);
        }
    }
}